unit lsAudioButton;

interface

uses
  Controls, XMLIntf, lsComponent, MPlayer, Classes, ExtCtrls;

type
  TlsAudioButton = class(TlsComponent)
  private
   FPlayer: TMediaPlayer;
   FFileName: string;
   FImage: string;
   FOverImage: string;
   FClickImage: string;
   FButton: TImage;
   FClicked: Boolean;
   procedure ResponsePlayer(ASendor: TObject);
   procedure ResponseClick(ASendor: TObject);
   procedure ResponseEnter(ASendor: TObject);
   procedure ResponseLeave(ASendor: TObject);
  protected
   procedure innerDoAction(AAction: String); override;
  public
    constructor Create(AParent: TWinControl;AlsConfig: IXMLNode);override;
    destructor  Destroy;override;
    procedure SetProperty(ANode: IXMLNode);override;
  end;

implementation

uses
  CommonUtils, SysUtils, Dialogs;

{ TlsAudioButton }

constructor TlsAudioButton.Create(AParent: TWinControl;AlsConfig: IXMLNode);
begin
  inherited;

  FPlayer:=TMediaPlayer.Create(nil);
  FPlayer.Parent := AParent;
  FPlayer.Visible := False;
end;

destructor TlsAudioButton.Destroy;
begin
  FPlayer.Free;

  inherited;
end;

procedure TlsAudioButton.InnerDoAction(AAction: String);
begin
  inherited;
  if AAction='show' then
  begin
    FClicked := False;
    FButton.Stretch := True;
    FButton.Visible := True;
    FButton.Picture.LoadFromFile(FImage);
  end
  else if AAction='hide' then
    FButton.Visible := False;
end;

procedure TlsAudioButton.ResponseClick(ASendor: TObject);
begin
  FClicked := True;
  FPlayer.OnNotify := nil;
  FPlayer.FileName := FFileName;
  FPlayer.Open;
  FPlayer.Play;
  FPlayer.OnNotify := ResponsePlayer;
  FButton.Picture.LoadFromFile(FClickImage);
end;

procedure TlsAudioButton.ResponseEnter(ASendor: TObject);
begin
  if not FClicked then
    FButton.Picture.LoadFromFile(FOverImage);
end;

procedure TlsAudioButton.ResponseLeave(ASendor: TObject);
begin
  if not FClicked then
    FButton.Picture.LoadFromFile(FImage);
end;

procedure TlsAudioButton.ResponsePlayer(ASendor: TObject);
begin
  if FPlayer.Mode = mpStopped then
    FireEvent('right');
end;

procedure TlsAudioButton.SetProperty(ANode: IXMLNode);
begin
  FFileName := PathOfTrain+ GetChildNodeValue(ANode, 'filename');
  FImage := PathOfTrain+ GetChildNodeValue(ANode, 'image');
  FOverImage := PathOfTrain+ GetChildNodeValue(ANode, 'overimage');
  FClickImage := PathOfTrain+ GetChildNodeValue(ANode, 'clickimage');
  FButton := TImage.Create(nil);
  FButton.Parent := FParent;
  FButton.Left := GetChildNodeValue(ANode, 'left');
  FButton.Top := GetChildNodeValue(ANode, 'top');
  FButton.Width := GetChildNodeValue(ANode, 'width');
  FButton.Height := GetChildNodeValue(ANode, 'height');
  FButton.Visible := False;
  FButton.OnClick := ResponseClick;
  FButton.OnMouseEnter := ResponseEnter;
  FButton.OnMouseLeave := ResponseLeave;
end;

end.
